package com.turnengine.client.global.user.bean;

import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.javabi.common.locale.LanguageCountry;
import java.io.IOException;

/**
 * The User Serializer.
 */
public class UserSerializer implements IUserSerializer {

	@Override
	public IUser readObject(IDataReader reader) throws IOException {

		// Read fields
		int id = reader.readInt();
		String name = reader.readString(true);
		String email = reader.readString(true);
		LanguageCountry language = reader.readEnum(LanguageCountry.class, true);

		// Create object
		IUser object = new User();
		object.setId(id);
		object.setName(name);
		object.setEmail(email);
		object.setLanguage(language);
		return object;
	}

	public void writeObject(IDataWriter writer, IUser object) throws IOException {

		// Get fields
		int id = object.getId();
		String name = object.getName();
		String email = object.getEmail();
		LanguageCountry language = object.getLanguage();

		// Write fields
		writer.writeInt(id);
		writer.writeString(name, true);
		writer.writeString(email, true);
		writer.writeEnum(language, true);
	}
}
